{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0bd32972-13e8-475c-9415-c50a50409568",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-09-04T14:17:41.716630Z",
     "iopub.status.busy": "2023-09-04T14:17:41.716258Z",
     "iopub.status.idle": "2023-09-04T14:17:42.097933Z",
     "shell.execute_reply": "2023-09-04T14:17:42.097255Z",
     "shell.execute_reply.started": "2023-09-04T14:17:41.716610Z"
    }
   },
   "source": [
    "### clone代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e35e35f2-89e4-4243-8c6c-bf3abe8b422c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "!git clone https://github.com/modelscope/modelscope-agent.git"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "cdf9e03b-a513-4b81-a75e-243f38a3c84a",
   "metadata": {},
   "source": [
    "### 安装特定依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3851d799-7162-4e73-acab-3c13cb1e43bd",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "! cd modelscope-agent && ! pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "048a2040",
   "metadata": {},
   "source": [
    "### 本地配置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08a515fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('modelscope-agent/examples/tools')\n",
    "\n",
    "import sys\n",
    "sys.path.append('../../')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d58efa40",
   "metadata": {},
   "source": [
    "### API_KEY管理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "70a4ffbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "print('请输入DASHSCOPE_API_KEY')\n",
    "os.environ['DASHSCOPE_API_KEY'] = input()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bfba97e",
   "metadata": {},
   "source": [
    "### 新工具注册"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d754353",
   "metadata": {},
   "outputs": [],
   "source": [
    "from modelscope_agent.tools.base import BaseTool\n",
    "from modelscope_agent.tools import register_tool\n",
    "\n",
    "@register_tool('RenewInstance')\n",
    "class AliyunRenewInstanceTool(BaseTool):\n",
    "    description = '续费一台包年包月ECS实例'\n",
    "    name = 'RenewInstance'\n",
    "    parameters: list = [{\n",
    "        'name': 'instance_id',\n",
    "        'description': 'ECS实例ID',\n",
    "        'required': True,\n",
    "        'type': 'string'\n",
    "    }, {\n",
    "        'name': 'period',\n",
    "        'description': '续费时长以月为单位',\n",
    "        'required': True,\n",
    "        'type': 'string'\n",
    "    }]\n",
    "\n",
    "    def call(self, params: str, **kwargs):\n",
    "        params = self._verify_args(params)\n",
    "        instance_id = params['instance_id']\n",
    "        period = params['period']\n",
    "        return str({'result': f'已完成ECS实例ID为{instance_id}的续费，续费时长{period}月'})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6626e101",
   "metadata": {},
   "source": [
    "### 构建agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6349d894",
   "metadata": {},
   "outputs": [],
   "source": [
    "from modelscope_agent.agents import RolePlay\n",
    "\n",
    "role_template = '你扮演一名ECS实例管理员，你需要根据用户的要求来满足他们'\n",
    "llm_config = {\n",
    "    'model': 'qwen-max', \n",
    "    'model_server': 'dashscope',\n",
    "    }\n",
    "function_list = ['RenewInstance']\n",
    "\n",
    "bot = RolePlay(function_list=function_list,llm=llm_config, instruction=role_template)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "623673e5",
   "metadata": {},
   "source": [
    "### 执行agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "102e46a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = bot.run(\"请帮我续费一台ECS实例，实例id是：i-rj90a7e840y5cde，续费时长10个月\", remote=False, print_info=True)\n",
    "text = ''\n",
    "for chunk in response:\n",
    "    text += chunk\n",
    "print(text)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
